package com.ruoyi.statistics.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.statistics.domain.GdArea;
import com.ruoyi.statistics.domain.GdType;
import com.ruoyi.statistics.domain.SearchVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.StatisticsGdData;
import com.ruoyi.statistics.service.IStatisticsGdDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 耕地Controller
 *
 * @author rww
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/statistics/gd")
public class StatisticsGdDataController extends BaseController
{
    @Autowired
    private IStatisticsGdDataService statisticsGdDataService;

    @GetMapping("/district")
    public AjaxResult listDistrict()
    {
        List<GdArea> list = statisticsGdDataService.selectStatisticsGdDistrict();
        return AjaxResult.success(list);
    }
    @GetMapping("/types")
    public AjaxResult listTypes()
    {
        List<String> list = statisticsGdDataService.selectStatisticsGdType();
        return AjaxResult.success(list);
    }
    @PostMapping("/statistics")
    public AjaxResult listStatistics(@RequestBody(required = false) SearchVo param)
    {
        StatisticsGdData statisticsGdDat = new StatisticsGdData();
        if(param.getTypes()!=null&&param.getTypes().size()>0){
            statisticsGdDat.setTypes(param.getTypes());
        }
        if(param.getAreas()!=null&&param.getAreas().size()>0){
            statisticsGdDat.setAreas(param.getAreas());
        }
        List<GdType> list = statisticsGdDataService.statisticsGdType(statisticsGdDat);
        return AjaxResult.success(list);
    }



    /**
     * 查询耕地列表
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(StatisticsGdData statisticsGdData)
    {
        startPage();
        List<StatisticsGdData> list = statisticsGdDataService.selectStatisticsGdDataList(statisticsGdData);
        return getDataTable(list);
    }

    /**
     * 导出耕地列表
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:export')")
    @Log(title = "耕地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StatisticsGdData statisticsGdData)
    {
        List<StatisticsGdData> list = statisticsGdDataService.selectStatisticsGdDataList(statisticsGdData);
        ExcelUtil<StatisticsGdData> util = new ExcelUtil<StatisticsGdData>(StatisticsGdData.class);
        util.exportExcel(response, list, "耕地数据");
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        try
        {
            ExcelUtil<StatisticsGdData> util = new ExcelUtil<StatisticsGdData>(StatisticsGdData.class);
            List<StatisticsGdData> statisticsGdData = util.importExcel(file.getInputStream());
            String message = statisticsGdDataService.importData(statisticsGdData);
            return success(message);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StatisticsGdData> util = new ExcelUtil<StatisticsGdData>(StatisticsGdData.class);
        util.importTemplateExcel(response, "数据");
    }

    /**
     * 获取耕地详细信息
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(statisticsGdDataService.selectStatisticsGdDataById(id));
    }

    /**
     * 新增耕地
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:add')")
    @Log(title = "耕地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StatisticsGdData statisticsGdData)
    {
        return toAjax(statisticsGdDataService.insertStatisticsGdData(statisticsGdData));
    }

    /**
     * 修改耕地
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:edit')")
    @Log(title = "耕地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StatisticsGdData statisticsGdData)
    {
        return toAjax(statisticsGdDataService.updateStatisticsGdData(statisticsGdData));
    }

    /**
     * 删除耕地
     */
    @PreAuthorize("@ss.hasPermi('statistics:data:remove')")
    @Log(title = "耕地", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(statisticsGdDataService.deleteStatisticsGdDataByIds(ids));
    }
}
